package br.com.virtual.base.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.inject.Inject;
import javax.inject.Named;

import br.com.virtual.cadastrar.model.Estado;
import br.com.virtual.cadastrar.service.EstadoService;

@Named("estadoConverter")
public class EstadoConverter implements Converter {

	@Inject
	private EstadoService estadoService;

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String str) {

		try {
			return str != null && str.length() > 0 ? this.estadoService
					.get(Long.valueOf(str)) : null;
		} catch (Exception e) {
			return null;
		}

	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object obj) {

		try {
			return obj != null ? ((Estado) obj).getId().toString() : null;
		} catch (Exception e) {
			return null;
		}
	}

}
